﻿using System;
using System.Collections.Generic;
using System.Text;
using SyncCommon;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace SyncWebSite
{

    /// <summary>
    /// 百姓网
    /// </summary>
    public class SyncBaiXing : SyncHouseBase
    {
        private string cd;
        private string ptk;
        private string targetID = "";
        private string logintime = "1346058712095";
        private List<string> retImages = new List<string>();
        private Dictionary<string, string> loginCookies = new Dictionary<string, string>();
        private string plugion = "Plugin%200%3A%20Default%20Plug-in%3B%20Provides%20functionality%20for%20installing%20third-party%20plug-ins%3B%20default_plugin%3B%20%28%3B%20*%3B%20%29.%20Plugin%201%3A%20Generic%20ActiveX%20Plugin%3B%20Generic%20ActiveX%20Plugin%3B%20Generic%20ActiveX%20Plugin%3B%20%28%3B%20application/x-oleobject%3B%20*%29.%20Plugin%202%3A%20Google%20Update%3B%20Google%20Update%3B%20npGoogleOneClick8.dll%3B%20%28%3B%20application/x-vnd.google.oneclickctrl.8%3B%20%29.%20Plugin%203%3A%20Java%20Deployment%20Toolkit%206.0.220.4%3B%20NPRuntime%20Script%20Plug-in%20Library%20for%20Java%28TM%29%20Deploy%3B%20npdeployJava1.dll%3B%20%28%3B%20application/java-deployment-toolkit%3B%20%29.%20Plugin%204%3A%20Java%28TM%29%20Platform%20SE%206%20U22%3B%20Next%20Generation%20Java%20Plug-in%201.6.0_22%20for%20Mozilla%20browsers%3B%20npjp2.dll%3B%20%28Java%20Applet%3B%20application/x-java-applet%3B%20%29%20%28JavaBeans%3B%20application/x-java-bean%3B%20%29%20%28%3B%20application/x-java-vm%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.1.1%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.1.1%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.1%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.1%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.2%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.2%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.1.3%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.1.3%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.1.2%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.1.2%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.3%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.3%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.2.2%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.2.2%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.2.1%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.2.1%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.3.1%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.3.1%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.4%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.4%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.4.1%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.4.1%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.4.2%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.4.2%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.5%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.5%3B%20%29%20%28%3B%20application/x-java-applet%3Bversion%3D1.6%3B%20%29%20%28%3B%20application/x-java-bean%3Bversion%3D1.6%3B%20%29%20%28%3B%20application/x-java-applet%3Bjpi-version%3D1.6.0_22%3B%20%29%20%28%3B%20application/x-java-bean%3Bjpi-version%3D1.6.0_22%3B%20%29%20%28%3B%20application/x-java-vm-npruntime%3B%20%29.%20Plugin%205%3A%20Microsoft%AE%20DRM%3B%20DRM%20Netscape%20Network%20Object%3B%20npdrmv2.dll%3B%20%28Network%20Interface%20Plugin%3B%20application/x-drm-v2%3B%20nip%29.%20Plugin%206%3A%20Microsoft%AE%20DRM%3B%20DRM%20Store%20Netscape%20Plugin%3B%20npwmsdrm.dll%3B%20%28Network%20Interface%20Plugin%3B%20application/x-drm%3B%20nip%29.%20Plugin%207%3A%20Microsoft%AE%20Windows%20Media%20Player%20Firefox%20Plugin%3B%20Windows%20Media%20Player%3B%20Microsoft%AE%20Windows%20Media%20Player%20Firefox%20Plugin%3B%20%28%3B%20application/x-ms-wmp%3B%20*%29%20%28%3B%20application/asx%3B%20*%29%20%28%3B%20video/x-ms-asf-plugin%3B%20*%29%20%28%3B%20application/x-mplayer2%3B%20*%29%20%28%3B%20video/x-ms-asf%3B%20asf%2Casx%2C*%29%20%28%3B%20video/x-ms-wm%3B%20wm%2C*%29%20%28%3B%20audio/x-ms-wma%3B%20wma%2C*%29%20%28%3B%20audio/x-ms-wax%3B%20wax%2C*%29%20%28%3B%20video/x-ms-wmv%3B%20wmv%2C*%29%20%28%3B%20video/x-ms-wvx%3B%20wvx%2C*%29%20%28%3B%20audio/mpeg%3B%20mp2%2Cmp3%2Cmpa%2Cmpeg%2Cmpg%29%20%28%3B%20video/mpeg%3B%20mpeg%2Cmpg%2Cm1v%2Cmpe%29.%20Plugin%208%3A%20Shockwave%20Flash%3B%20Shockwave%20Flash%2010.0%20r45%3B%20NPSWF32.dll%3B%20%28Adobe%20Flash%20movie%3B%20application/x-shockwave-flash%3B%20swf%29%20%28FutureSplash%20movie%3B%20application/futuresplash%3B%20spl%29.%20Plugin%209%3A%20Windows%20Presentation%20Foundation%3B%20Windows%20Presentation%20Foundation%20%28WPF%29%20plug-in%20for%20Mozilla%20browsers%3B%20NPWPF.dll%3B%20%28XAML%20Browser%20Application%3B%20application/x-ms-xbap%3B%20xbap%29%20%28XAML%20Document%3B%20application/xaml+xml%3B%20xaml%29.%20Plugin%2010%3A%20npruntime%20scriptable%20example%20plugin%3B%20nprt%3B%20npuuseep.dll%3B%20%28nprt%3B%20application/uusee-vod-scriptable-plugin%3B%20rts%29.%20%7Ccomp%3A";
        private string fonts = "Marlett%2CArial%2CArial%20CE%2CArial%20CYR%2CArial%20Greek%2CArial%20TUR%2CArial%20Baltic%2CCourier%20New%2CCourier%20New%20CE%2CCourier%20New%20CYR%2CCourier%20New%20Greek%2CCourier%20New%20TUR%2CCourier%20New%20Baltic%2CLucida%20Console%2CLucida%20Sans%20Unicode%2CTimes%20New%20Roman%2CTimes%20New%20Roman%20CE%2CTimes%20New%20Roman%20CYR%2CTimes%20New%20Roman%20Greek%2CTimes%20New%20Roman%20TUR%2CTimes%20New%20Roman%20Baltic%2CWingdings%2CSymbol%2CVerdana%2CArial%20Black%2CComic%20Sans%20MS%2CImpact%2CGeorgia%2CFranklin%20Gothic%20Medium%2CPalatino%20Linotype%2CTahoma%2CTrebuchet%20MS%2CWebdings%2CMV%20Boli%2C%u5B8B%u4F53%2C%u65B0%u5B8B%u4F53%2C%u5B8B%u4F53-PUA%2C%u9ED1%u4F53%2C%u6977%u4F53_GB2312%2C%u4EFF%u5B8B_GB2312%2CMicrosoft%20Sans%20Serif%2CMS%20Gothic%2CMS%20PGothic%2CMS%20UI%20Gothic%2CGulim%2CGulimChe%2CDotum%2CDotumChe%2CMingLiU%2CPMingLiU%2C%u65B9%u6B63%u59DA%u4F53%2C%u5FAE%u8F6F%u96C5%u9ED1%2C%u96B6%u4E66%2C%u5E7C%u5706%2C%u534E%u6587%u884C%u6977%2C%u534E%u6587%u65B0%u9B4F%2CArial%20Narrow%2CBook%20Antiqua%2CBookman%20Old%20Style%2CMonotype%20Corsiva%2CNina%2CSegoe%20Condensed%2CHaettenschweiler%2CMS%20Outlook%2CCentury%20Gothic%2CBookshelf%20Symbol%207%2CMS%20Reference%20Sans%20Serif%2CMS%20Reference%20Specialty%2CBradley%20Hand%20ITC%2CFreestyle%20Script%2CFrench%20Script%20MT%2CJuice%20ITC%2CKristen%20ITC%2CLucida%20Handwriting%2CMistral%2CPapyrus%2CPristina%2CTempus%20Sans%20ITC%2CGaramond%2CAgency%20FB%2CArial%20Rounded%20MT%20Bold%2CBlackadder%20ITC%2CBodoni%20MT%2CBodoni%20MT%20Black%2CBodoni%20MT%20Condensed%2CCalisto%20MT%2CCastellar%2CCentury%20Schoolbook%2CCopperplate%20Gothic%20Bold%2CCopperplate%20Gothic%20Light%2CCurlz%20MT%2CEdwardian%20Script%20ITC%2CElephant%2CEngravers%20MT%2CEras%20Bold%20ITC%2CEras%20Demi%20ITC%2CEras%20Light%20ITC%2CEras%20Medium%20ITC%2CFelix%20Titling%2CForte%2CFranklin%20Gothic%20Book%2CFranklin%20Gothic%20Demi%2CFranklin%20Gothic%20Demi%20Cond%2CFranklin%20Gothic%20Heavy%2CFranklin%20Gothic%20Medium%20Cond%2CGigi%2CGill%20Sans%20MT%2CGill%20Sans%20MT%20Condensed%2CGill%20Sans%20Ultra%20Bold%2CGill%20Sans%20Ultra%20Bold%20Condensed%2CGill%20Sans%20MT%20Ext%20Condensed%20Bold%2CGloucester%20MT%20Extra%20Condensed%2CGoudy%20Old%20Style%2CGoudy%20Stout%2CImprint%20MT%20Shadow%2CLucida%20Sans%2CLucida%20Sans%20Typewriter%2CMaiandra%20GD%2COCR%20A%20Extended%2CPalace%20Script%20MT%2CPerpetua%2CPerpetua%20Titling%20MT%2CRage%20Italic%2CRockwell%2CRockwell%20Condensed%2CRockwell%20Extra%20Bold%2CScript%20MT%20Bold%2CTw%20Cen%20MT%2CTw%20Cen%20MT%20Condensed%2CTw%20Cen%20MT%20Condensed%20Extra%20Bold%2CAlgerian%2CBaskerville%20Old%20Face%2CBauhaus%2093%2CBell%20MT%2CBerlin%20Sans%20FB%2CBerlin%20Sans%20FB%20Demi%2CBernard%20MT%20Condensed%2CBodoni%20MT%20Poster%20Compressed%2CBritannic%20Bold%2CBroadway%2CBrush%20Script%20MT%2CCalifornian%20FB%2CCentaur%2CChiller%2CColonna%20MT%2CCooper%20Black%2CFootlight%20MT%20Light%2CHarlow%20Solid%20Italic%2CHarrington%2CHigh%20Tower%20Text%2CJokerman%2CKunstler%20Script%2CLucida%20Bright%2CLucida%20Calligraphy%2CLucida%20Fax%2CMagneto%2CMatura%20MT%20Script%20Capitals%2CModern%20No.%2020%2CNiagara%20Engraved%2CNiagara%20Solid%2COld%20English%20Text%20MT%2COnyx%2CParchment%2CPlaybill%2CPoor%20Richard%2CRavie%2CInformal%20Roman%2CShowcard%20Gothic%2CSnap%20ITC%2CStencil%2CViner%20Hand%20ITC%2CVivaldi%2CVladimir%20Script%2CWide%20Latin%2CArial%20Unicode%20MS%2CMS%20Mincho%2CMT%20Extra%2C%u65B9%u6B63%u8212%u4F53%2C%u534E%u6587%u5F69%u4E91%2C%u534E%u6587%u4EFF%u5B8B%2C%u534E%u6587%u7425%u73C0%2C%u534E%u6587%u6977%u4F53%2C%u534E%u6587%u96B6%u4E66%2C%u534E%u6587%u5B8B%u4F53%2C%u534E%u6587%u7EC6%u9ED1%2C%u534E%u6587%u4E2D%u5B8B%2CCalibri%2CCambria%2CCambria%20Math%2CCandara%2CConsolas%2CConstantia%2CCorbel%2CSegoe%20UI%2CCentury%2CWingdings%202%2CWingdings%203";
        private string MobileNumber = "";
        private string LoadTimer = "";
        private string CityName = "shanghai";
        public SyncBaiXing(string user, string password)
            : base(user, password)
        {
            
        }

        #region SyncHouseBase

        public override bool CheckLogin()
        {
            WebRequest request = new WebRequest("http://www.baixing.com/auth/denglu/?src=headerHome", FormType.FormUrlEncode);
            request.Cookies = loginCookies;
            //request.Cookies["__djfb"] = "1";
            request.Cookies["__pfk"] = "1";
            request.AddStringCookies("__trackId=135098109224097;");
            request.AllowRedirect = false;
            request.AddContent("identity", UserName);
            request.AddContent("password", Password);
            request.AddContent("type", "login");
            request.AddContent("redirect", "http://shanghai.baixing.com/");
            request.AddContent("refer", "");
            string ret = request.SendWaitString();
            if (request.TransferLocation == "http://shanghai.baixing.com/")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        

        public override bool AddSaleHouse(HouseInfo info, ref string targetID)
        {
            UpdateCurrentStatus(StandardProgress.InitalData);

            string error = "";
            try
            {
                if (!CheckLogin())
                {
                    UpdateCurrentStatus(StandardProgress.Failed, "登录失败");
                    return false;
                }
                else
                {
                    string errorInfor = "";
                    if (!CommonCheck(info, true, ref errorInfor))
                    {
                        UpdateCurrentStatus(StandardProgress.Failed, errorInfor);
                        return false;
                    }
                    GetCityName(info);
                    if (this.GetTotalPublished(CityName, "ershoufang"))
                    {

                        WebRequest webReq = new WebRequest("http://" + CityName + ".baixing.com/");
                        webReq.Cookies = loginCookies;
                        webReq.GetResouceStr();
                        //string url = "http://www.baixing.com/fabu/" + CityName + "/?src=header&category=fang";
                        webReq = new WebRequest("http://www.baixing.com/fabu/?src=header&city=" + CityName + "&category=ershoufang");
                        webReq.Cookies = loginCookies;
                        string input = webReq.GetResouceStr();
                        //RefreshArrow(input);
                        RefreshArrow("m35763");
                        RefreshArrow("m35623");
                        //RefreshArrow("m35769");
                       
                        WebRequest request = new WebRequest("http://www.baixing.com/fabu/post/?city=" + CityName, FormType.FormUrlEncode);
                        request.AllowRedirect = false;
                        request.Cookies = loginCookies;
                        request.AddStringCookies("__utma=63213232.336224647.1350981089.1351394370.1351400719.13; __utmz=63213232.1351240055.9.4.utmcsr=shanghai.baixing.com|utmccn=(referral)|utmcmd=referral|utmcct=/ershoufang/a245720423.html;  __utmc=63213232;  __utmb=63213232.1.10.1351400719; __uuid=113471640154696.c836e; ");
                        Regex regex = new Regex("<input type=\"hidden\" name=\"(?<name>[\\s\\S]*?)\"[\\S\\s]*?value=\"(?<value>[\\s\\S]*?)\" />");
                        foreach (Match match in regex.Matches(input))
                        {
                            if (match.Groups["name"].Value == "plgs")
                            {
                                request.AddContent(match.Groups["name"].Value.ToString(), plugion);
                            }
                            else
                            {
                                if (match.Groups["name"].Value == "fonts")
                                {
                                    request.AddContent(match.Groups["name"].Value.ToString(), fonts);
                                    continue;
                                }
                                if (!match.Groups["name"].Value.ToString().Contains("image[]"))
                                {
                                    request.AddContent(match.Groups["name"].Value.ToString(), match.Groups["value"].Value.ToString());
                                }
                            }
                        }
                        CalculateTimeLoader(input, request);
                        //request.AddPureStr("category=&category=&category=ershoufang&wanted=0&title=%E9%9D%A0%E8%BF%91%E5%AE%B6%E4%B9%90%E5%AF%8C&%E5%9C%B0%E5%8C%BA_area2=%E6%B5%A6%E4%B8%9C&%E5%9C%B0%E5%8C%BA_area2=m7255&%E5%9C%B0%E5%8C%BA_area3=%E5%8C%97%E8%94%A1&%E5%9C%B0%E5%8C%BA_area3=m2223&%E5%85%B7%E4%BD%93%E5%9C%B0%E7%82%B9=%E6%9B%B9%E9%98%B3%E4%B8%89%E6%9D%91&%E5%85%B7%E4%BD%93%E5%9C%B0%E7%82%B9=%E6%9B%B9%E9%98%B3%E4%B8%89%E6%9D%91&%E8%A3%85%E4%BF%AE%E6%83%85%E5%86%B5=%E6%AF%9B%E5%9D%AF%E6%88%BF&%E6%88%BF%E5%9E%8B=2%E5%AE%A4&%E6%88%BF%E5%9E%8B%E5%8E%85=1%E5%8E%85&%E6%88%BF%E5%9E%8B%E5%8D%AB=1%E5%8D%AB&%E9%9D%A2%E7%A7%AF=63&%E6%A5%BC%E5%B1%82=3&%E4%BB%B7%E6%A0%BC=273&faburen=m33663&description=&image%5B%5D=%2F443b7bf0c9c16a6dc352524a9ee77672.jpg&%E8%81%94%E7%B3%BB%E4%BA%BA=%E5%93%88%E5%93%88&contact=13774255334");
                        FillPostData(info, request,true);
                        //RefreshAll();
                        //ValidateMobile(info);
                        Thread.Sleep(4000);
                        request.AddStringCookies("__trackId=135098109224097; __utma=63213232.336224647.1350981089.1351394370.1351400719.13; __utmz=63213232.1351240055.9.4.utmcsr=shanghai.baixing.com|utmccn=(referral)|utmcmd=referral|utmcct=/ershoufang/a245720423.html;  __utmc=63213232;  __utmb=63213232.1.10.1351400719; __uuid=113471640154696.c836e;");
                        //request.AddStringCookies("__trackId=135098109224097; __utma=63213232.336224647.1350981089.1351607751.1351611174.19; __utmz=63213232.1351240055.9.4.utmcsr=shanghai.baixing.com|utmccn=(referral)|utmcmd=referral|utmcct=/ershoufang/a245720423.html; __city=shanghai; __u=89578562; __n=wahym_5; __c=bbf3747dfc47ef85fcbc5db626db03cff31ec8e4; __uuid=113471640154696.c836e; __pfk=1; __utmc=63213232; __djfb=1; __utmb=63213232.1.10.1351611174");
                        
                         string ret =  request.SendWaitString();
                        //如果发布信息中的手机号码跟注册号码不同。。需要验证密码
                        if (ret.Contains("您信息中的手机号与注册手机号不同"))
                        {
                            UpdateCurrentStatus(StandardProgress.Failed, "百姓网要求发布信息中的手机号码必须跟注册号码相同");
                            return false;
                        }
                        if (ret.Contains("recheck"))
                        {
                            UpdateCurrentStatus(StandardProgress.Failed, "你的账号没有经过手机验证");
                            return false;
                        }
                        if (ret.Contains("fabu/bad/?adId"))
                        {
                            UpdateCurrentStatus(StandardProgress.Failed, "系统发现您以前发布过类似的信息");
                            return false;
                        }
                        if (ret.Contains("postSuccess=1"))
                        {
                            string targetUrl = request.TransferLocation.Substring(0,request.TransferLocation.LastIndexOf('?'));
                            Regex regex3 = new Regex("/a(?<houseid>\\d*?)\\.");

                            targetID = regex3.Match(targetUrl).Groups["houseid"].Value;
                            UpdateCurrentStatus(StandardProgress.Sucess, GetImageError(info), targetID, targetUrl);
                            return true;
                        }
                        if (ret.Contains("发布成功！") || (ret.Contains("无法显示") && !ret.Contains("发布过类似的信息")))
                        {
                            if (ret.Contains("发布成功！"))
                            {
                                string targetUrl = request.ResponUri.Substring(0, request.ResponUri.IndexOf("?"));
                                Regex regex3 = new Regex("/a(?<houseid>\\d*?)\\.");


                                targetID = regex3.Match(targetUrl).Groups["houseid"].Value;
                                UpdateCurrentStatus(StandardProgress.Sucess, GetImageError(info), targetID, targetUrl);
                                return true;
                            }
                            if (ret.Contains("无法显示"))
                            {
                                Regex regex2 = null;
                                if (ret.Contains("该信息不符合"))
                                {
                                    regex2 = new Regex(@"该信息不符合[\s\S]*?>(?<Msg>[\S\s]*?)</");
                                    error = "发布但无法显示,原因:" + regex2.Match(ret).Groups["Msg"].Value;
                                }
                                else
                                {
                                    if (ret.Contains("该信息无法在信息列表页显示，原因是该信息审核未通过"))
                                    {
                                        error = "信息审核未通过.请去百姓网查看。";
                                    }
                                    else
                                    {
                                        error = "";
                                    }
                                }
                            }
                            else
                            {
                                error = "未知错误";
                            }
                        }
                        else
                        {
                            Regex regex4 = new Regex("<div id=\"left\"[\\s\\S]*?>[\\S\\s]*?<p[\\s\\S]*?>(?<Msg>[\\s\\S]*?)</p>");
                            string str8 = regex4.Match(ret).Groups["Msg"].Value;
                            if (str8 == "")
                            {
                                regex4 = new Regex(@"After(?<Msg>[\S\s]*?)'");
                                str8 = regex4.Match(ret).Groups["Msg"].Value;
                                if (str8.Contains("发布出错"))
                                {
                                    regex4 = new Regex(@"错误信息：(?<Msg>[\s\S]*?)\)");
                                    if (regex4.Match(ret).Groups["Msg"].Value != "")
                                    {
                                        str8 = regex4.Match(ret).Groups["Msg"].Value;
                                    }
                                }
                            }
                            error = str8;
                            if (error == "")
                            {
                                error = "该信息不符合百姓网的规则或发布超时等";
                            }
                            if (error.Contains("直接封杀账号"))
                            {
                                error = "该信息不符合百姓网的规则或发布超时等";
                            }
                        }

                    }
                    else
                    {
                        error = "你发布的房源数量已超过限制";
                    }
                }
            }
            catch (System.Net.WebException exp)
            {
                error = WebError;
            }
            catch (Exception exp)
            {
                LogCenter.Log("百姓网信息提交", exp);
                error = exp.Message;
            }
            UpdateCurrentStatus(StandardProgress.Failed, error);
            return false;
        }

        private void RefreshAll()
        {
            RefreshSignle("http://www.baixing.com/pages/ic.php?type=uv&keys[]=JS%E8%BF%9B%E5%85%A5%E5%8F%91%E5%B8%83&r=" + (new Random().Next(10000, 20000)).ToString());
            RefreshSignle("http://www.baixing.com/fabu/metahtml/?city=shanghai&category=ershoufang&city=shanghai&r=" + (new Random().Next(10000, 20000)).ToString());
            RefreshSignle("http://www.baixing.com/ajax/fabu/totalAdLimit/?categoryName=ershoufang&cityName=shanghai");
            RefreshSignle("http://www.baixing.com/pages/evtlog.php?key=enterfabulist&id=135161337346377");
            RefreshSignle("http://www.baixing.com/pages/evtlog.php?key=enterfabuhome&id=135161337346377");
            RefreshSignle("http://www.baixing.com/pages/evtlog.php?key=fabuselcate&id=135161337346377");

            RefreshSignle("http://static.baixing.net/media/js/swfupload__44076__.js?_=1351613374207");
            RefreshSignle("http://www.baixing.com/image_up/");


            WebRequest request = new WebRequest("http://gary.baixing.com/image.php?ct=21&en=ershoufang&ref=&url=http%3A//www.baixing.com/fabu/%3Fsrc%3Dheader%26city%3Dshanghai%26category%3Dershoufang&tk1=form-enhance0&tk2=listing_nosearch9&tk3=hotlink9&tk4=ershou_contact9&cv5=135098109224097");
            request.AddStringCookies("__trackId=135098109224097; kjj_log_log_id=13509810931119035490; __utma=63213232.336224647.1350981089.1351607751.1351611174.19; __utmz=63213232.1351240055.9.4.utmcsr=shanghai.baixing.com|utmccn=(referral)|utmcmd=referral|utmcct=/ershoufang/a245720423.html; __city=shanghai; __u=89578562; __n=wahym_5; __c=bbf3747dfc47ef85fcbc5db626db03cff31ec8e4; __uuid=113471640154696.c836e; __pfk=1; __utmc=63213232; kjj_log_session_id=13516027201248026731; kjj_log_origin=1; kjj_log_session_depth=8; __djfb=1; __utmb=63213232.4.10.1351611174");
            request.GetResouceStr();
        }
        private void RefreshSignle(string url)
        {
            WebRequest request = new WebRequest(url);
            request.Cookies = loginCookies;
            request.GetResouceStr();
        }

        private void TokenRule(Regex reg, string input)
        {
            MatchCollection matchs = reg.Matches(input);
            if (matchs != null)
            {  
                foreach (Match ma in matchs)
                {
                    WebRequest validateRequest = new WebRequest(ma.Groups["url"].Value);
                    validateRequest.Cookies = loginCookies;
                    validateRequest.GetResouceStr();
                }
            }
        }

        private void CalculateTimeLoader(string inputHtml, WebRequest request)
        {
            Regex regex = new Regex(@"<script>(?<sc>.*?;)document.write");
            Match ma= regex.Match(inputHtml);
            if (ma.Success)
            {
                object timeloader = HtmlHelper.ExcuteScript(" var ___=0,____=0,__=0; " + ma.Groups["sc"].Value);
                if (timeloader != null)
                {
                    int ret =0;
                    if (int.TryParse(timeloader.ToString(), out ret))
                    {
                        request.AddContent("loadTimer", ret.ToString());
                    }
                }
            }
        }
        private void ValidateMobile(HouseInfo info)
        {
            string ra = new Random().Next(0, 99999).ToString();
            WebRequest validateRequest = new WebRequest("http://www.baixing.com/pages/ic.php?type=uv&keys[]=" + HtmlHelper.UrlEncode("点击发布按钮", Encoding.UTF8) +
                "&r=" + ra);
            validateRequest.Cookies = loginCookies;
            string valret = validateRequest.GetResouceStr();

            validateRequest = new WebRequest("http://www.baixing.com/ajax/Valid/isVerifyMobileHoster?t=1351005025496&mobile=" + MobileNumber);
            validateRequest.Cookies = loginCookies;
            valret = validateRequest.GetResouceStr();
            loginCookies["__djfb"] = "1";
        }

        private void RefreshArrow(string input)
        {
           // string saleValue = "m35769";
            //WebRequest request = new WebRequest("http://www.baixing.com/ajax/tag/select/?category=&cityEnglishName=" + CityName + "&key=category&value=" + cate, FormType.FormUrlEncode);
            //request.Cookies = loginCookies;
            //string input = request.SendWaitString();
           // input = HtmlHelper.DecodingJSON(input);
            Regex regex = new Regex(@"http:\\/\\/www.baixing.com\\/images\\/arrowhead.gif\?id=(?<ids>\d+)");
            if (regex.IsMatch(input))
            {
                string str4 = regex.Match(input).Groups["ids"].Value ?? "";
                if (!string.IsNullOrEmpty(str4))
                {
                    WebRequest request = new WebRequest(string.Format("http://www.baixing.com/images/arrowhead.gif?id={0}", str4));
                    request.Cookies = loginCookies;
                    request.SetHeaderValue("Referer", "http://www.baixing.com/fabu/?src=header&city=" + CityName);
                    string tempRet = request.GetResouceStr();
                }
            }
        }

        private void FillPostData(HouseInfo info, WebRequest request,bool isSale)
        {
            
           // request.AddContent("loadTimer", "402");
            request.AddContent("category", "");
            request.AddContent("category", "");
            if (isSale)
            {
                request.AddContent("category", "ershoufang");
            }
            else
            {
                request.AddContent("category", "zhengzu");
            }

        
            request.AddContent("wanted", "0");//0代表出售 。1代表求购


            request.AddContent("title", GetTitle(info.Title));
            MapEstate(info, request);
            request.AddContent("具体地点", info.Estate);
            request.AddContent("具体地点", info.Estate);
            request.AddContent("装修情况", GetDecoration(info));
            request.AddContent("房型", GetHouseType(info.Room, "室"));
            request.AddContent("房型厅", GetHouseType(info.Hall, "厅"));
            request.AddContent("房型卫", GetHouseType(info.WashRoom, "卫"));
            //request.AddContent("物业类型", GetHouseType(info));
            request.AddContent("面积", GetArea(info));
            request.AddContent("楼层", info.CurrentFloor);

            //request.AddContent("朝向", GetToward(info));
            //request.AddContent("建造年代", info.Age + "年");

            if (isSale)
            {
                request.AddContent("价格", GetPrice(info));
            }
            else
            {
                request.AddContent("价格", info.MonthPrice);
            }
  
            request.AddContent("faburen", "m33663");//经纪人 m33660代表个人
            request.AddContent("description", "");//info.Description
            
            //request.AddContent("看房时间", "随时看房");

            UpdateCurrentStatus(StandardProgress.PosData);
            //同步图片
            SyncImage(info);
            foreach (string imgurl in retImages)
            {
                request.AddContent("image[]", imgurl);
            }
            MobileNumber = GetMobile(info);
            request.AddContent("联系人", info.Name);
            request.AddContent("contact", MobileNumber);
       
            request.AddContent("double_size", "0");
            request.AddContent("promot", "free");

           
        }

        public override bool AddRentHouse(HouseInfo info, ref string targetID)
        {
            UpdateCurrentStatus(StandardProgress.InitalData);

            string error = "";
            try
            {
                if (!CheckLogin())
                {
                    UpdateCurrentStatus(StandardProgress.Failed, "登录失败");
                    return false;
                }
                else
                {
                    string errorInfor = "";
                    if (!CommonCheck(info, false, ref errorInfor))
                    {
                        UpdateCurrentStatus(StandardProgress.Failed, errorInfor);
                        return false;
                    }
                    GetCityName(info);
                    if (this.GetTotalPublished(CityName, "zhengzu"))
                    {
                        WebRequest webReq = new WebRequest("http://" + CityName + ".baixing.com/");
                        webReq.Cookies = loginCookies;
                        webReq.GetResouceStr();
                        webReq = new WebRequest("http://www.baixing.com/fabu/?src=header&city=" + CityName + "&category=zhengzu");
                        webReq.Cookies = loginCookies;
                        string input = webReq.GetResouceStr();
                        RefreshArrow("m35763");
                        RefreshArrow("m35623");
                       WebRequest  request = new WebRequest("http://www.baixing.com/fabu/post/?city=" + CityName, FormType.FormUrlEncode);
                        request.Cookies = loginCookies;
                        request.AllowRedirect = false;
                        Regex regex = new Regex("<input type=\"hidden\" name=\"(?<name>[\\s\\S]*?)\"[\\S\\s]*?value=\"(?<value>[\\s\\S]*?)\" />");
                        foreach (Match match in regex.Matches(input))
                        {
                            if (match.Groups["name"].Value == "plgs")
                            {
                                request.AddContent(match.Groups["name"].Value.ToString(), plugion);
                            }
                            else
                            {
                                if (match.Groups["name"].Value == "fonts")
                                {
                                    request.AddContent(match.Groups["name"].Value.ToString(), fonts);
                                    continue;
                                }
                                if (!match.Groups["name"].Value.ToString().Contains("image[]"))
                                {
                                    request.AddContent(match.Groups["name"].Value.ToString(), match.Groups["value"].Value.ToString());
                                }
                            }
                        }
                        #region Rent related information

                        
                        request.AddContent("keywords", info.SearchKey);
                        request.AddContent("分类", GetHireType(info));
                        request.AddContent("租用面积", info.Area);
                        
                        request.AddContent("押金方式", GetPayment(info));
                        #endregion
                        CalculateTimeLoader(input, request);
                        FillPostData(info, request,false);
                        request.AddStringCookies("__trackId=135098109224097; __utma=63213232.336224647.1350981089.1351394370.1351400719.13; __utmz=63213232.1351240055.9.4.utmcsr=shanghai.baixing.com|utmccn=(referral)|utmcmd=referral|utmcct=/ershoufang/a245720423.html;  __utmc=63213232;  __utmb=63213232.1.10.1351400719; __uuid=113471640154696.c836e;");
                        Thread.Sleep(4000);
                        string ret = request.SendWaitString();
                        //如果发布信息中的手机号码跟注册号码不同。。需要验证密码
                        if (ret.Contains("您信息中的手机号与注册手机号不同"))
                        {
                            UpdateCurrentStatus(StandardProgress.Failed, "百姓网要求发布信息中的手机号码必须跟注册号码相同");
                            return false;
                        }
                        if (ret.Contains("recheck"))
                        {
                            UpdateCurrentStatus(StandardProgress.Failed, "你的账号没有经过手机验证");
                            return false;
                        }
                        if (ret.Contains("fabu/bad/?adId"))
                        {
                            UpdateCurrentStatus(StandardProgress.Failed, "系统发现您以前发布过类似的信息");
                            return false;
                        }
                        if (ret.Contains("postSuccess=1"))
                        {
                            string targetUrl = request.TransferLocation.Substring(0, request.TransferLocation.LastIndexOf('?'));
                            Regex regex3 = new Regex("/a(?<houseid>\\d*?)\\.");

                            targetID = regex3.Match(targetUrl).Groups["houseid"].Value;
                            UpdateCurrentStatus(StandardProgress.Sucess, GetImageError(info), targetID, targetUrl);
                            return true;
                        }

                        if (ret.Contains("发布成功！") || (ret.Contains("无法显示") && !ret.Contains("发布过类似的信息")))
                        {
                            if (ret.Contains("发布成功！"))
                            {
                                string targetUrl = request.ResponUri.Substring(0, request.ResponUri.IndexOf("?"));
                                Regex regex3 = new Regex("/a(?<houseid>\\d*?)\\.");


                                targetID = regex3.Match(targetUrl).Groups["houseid"].Value;
                                UpdateCurrentStatus(StandardProgress.Sucess, GetImageError(info), targetID, targetUrl);
                                return true;
                            }
                            if (ret.Contains("无法显示"))
                            {
                                Regex regex2 = null;
                                if (ret.Contains("该信息不符合"))
                                {
                                    regex2 = new Regex(@"该信息不符合[\s\S]*?>(?<Msg>[\S\s]*?)</");
                                    error = "发布但无法显示,原因:" + regex2.Match(ret).Groups["Msg"].Value;
                                }
                                else
                                {
                                    if (ret.Contains("该信息无法在信息列表页显示，原因是该信息审核未通过"))
                                    {
                                        error = "信息审核未通过.请去百姓网查看。";
                                    }
                                    else
                                    {
                                        error = "";
                                    }
                                }
                            }
                            else
                            {
                                error = "未知错误";
                            }
                        }
                        else
                        {
                            Regex regex4 = new Regex("<div id=\"left\"[\\s\\S]*?>[\\S\\s]*?<p[\\s\\S]*?>(?<Msg>[\\s\\S]*?)</p>");
                            string str8 = regex4.Match(ret).Groups["Msg"].Value;
                            if (str8 == "")
                            {
                                regex4 = new Regex(@"After(?<Msg>[\S\s]*?)'");
                                str8 = regex4.Match(ret).Groups["Msg"].Value;
                                if (str8.Contains("发布出错"))
                                {
                                    regex4 = new Regex(@"错误信息：(?<Msg>[\s\S]*?)\)");
                                    if (regex4.Match(ret).Groups["Msg"].Value != "")
                                    {
                                        str8 = regex4.Match(ret).Groups["Msg"].Value;
                                    }
                                }
                            }
                            error = str8;
                            if (error == "")
                            {
                                error = "该信息不符合百姓网的规则或发布超时等";
                            }
                            if (error.Contains("直接封杀账号"))
                            {
                                error = "该信息不符合百姓网的规则或发布超时等";
                            }
                        }
                    }
                    else
                    {
                        error = "你发布的房源数量已超过限制";
                    }

                }
            }
            catch (System.Net.WebException exp)
            {
                error = WebError;
            }
            catch (Exception exp)
            {
                LogCenter.Log("百姓网信息提交", exp);
                error = exp.Message;
            }
            UpdateCurrentStatus(StandardProgress.Failed, error);
            return false;
        }

        public override bool DeleteSaleHouse(HouseInfo info, string targetID)
        {
            UpdateCurrentStatus(StandardProgress.InitalData);
            try
            {
                if (CheckLogin())
                {
                    Random r = new Random();
                    WebRequest request = new WebRequest("http://www.baixing.com/ajax/xinxi/confirmDelete/?adId=" + targetID + "&view=0&type=1&r=" + r.Next(1000, 9999).ToString());
                    request.Cookies = loginCookies;
                    request.SetHeaderValue("X-Requested-With", "XMLHttpRequest");
                    request.SetHeaderValue("Referer", "http://www.baixing.com/wo/postings/");
                    UpdateCurrentStatus(StandardProgress.PosData);
                    string status = request.GetResouceStr();
                    status = HtmlHelper.DecodingJSON(status);
                    if (status.Contains("删除成功"))
                    {
                        UpdateCurrentStatus(StandardProgress.Sucess, "");
                        return true;
                    }
                    else
                    {
                        UpdateCurrentStatus(StandardProgress.Failed, "删除失败");
                        LogCenter.LogMessage("删除信息失败。提交值ID:" + targetID ?? "" + "返回值:" + status ?? "");
                        return false;
                    }
                }
                else
                {
                    UpdateCurrentStatus(StandardProgress.Failed, "登陆失败");
                    return false;
                }
            }
            catch (System.Net.WebException exp)
            {
                UpdateCurrentStatus(StandardProgress.Failed, WebError);
                return false;
            }
        }

        public override bool DeleteRentHouse(HouseInfo info, string targetID)
        {
            return DeleteSaleHouse(info, targetID);
        }

        public override bool RefreshSaleHouse(HouseInfo info, string targetID)
        {
            UpdateCurrentStatus(StandardProgress.InitalData);
            try
            {
                if (CheckLogin())
                {
                    WebRequest request = new WebRequest("http://www.baixing.com/ajax/Pay/refresh/?view=0&type=13&adId=" + targetID);
                    request.Cookies = loginCookies;
                    UpdateCurrentStatus(StandardProgress.PosData);
                    string status = request.GetResouceStr();
                    status = HtmlHelper.DecodingJSON(status);
                    if (status.Contains("刷新成功"))
                    {
                        UpdateCurrentStatus(StandardProgress.Sucess, "");
                        return true;
                    }
                    else
                    {
                        string error = "";
                        if (status.Contains("不足以支付"))
                        {
                            error = "您的百姓网账户余额不足";
                        }
                        else
                        {
                            error = "刷新失败。请检查百姓网账户余额";
                        }
                        UpdateCurrentStatus(StandardProgress.Failed, string.IsNullOrEmpty(error) ? "刷新信息失败" : error);
                        LogCenter.LogMessage("刷新信息失败。提交值ID:" + targetID ?? "");
                        return false;
                    }
                }
                else
                {
                    UpdateCurrentStatus(StandardProgress.Failed, "登陆失败");
                    return false;
                }
            }
            catch (System.Net.WebException exp)
            {
                UpdateCurrentStatus(StandardProgress.Failed, WebError);
                return false;
            }
        }
        public override bool RefreshRentHouse(HouseInfo info, string targetID)
        {
            return RefreshSaleHouse(info, targetID);
        }
        #endregion

        #region HelpFunction

        public string GetTitle(string title)
        {
            //百姓网 TITLE 有长度限制
            if (title.Length > 25)
            {
               return title.Substring(0, 24);
            }
            return title;
        }

        public void SyncImage(HouseInfo info)
        {
            retImages.Clear();
            errorImage = 0;
            if (info.Images != null && info.Images.Length > 0)
            {
                string status = "";
                foreach (string url in info.Images)
                {
                    try
                    {
                        WebRequest request = new WebRequest(url);
                        Stream stream = request.GetResouce();
                        string name = url.Substring(url.LastIndexOf('/') + 1);


                        request = new WebRequest("http://www.baixing.com/image_upload/?s", FormType.MultiPart);
                        request.AddContent("name", name);
                        request.AddFile(name, "file", stream);
                        string k = request.SendWaitString();
                        if (!k.Contains("图片上传成功"))
                        {
                            errorImage++;
                        }
                        else
                        {
                            Regex regex = new Regex("http://img.baixing.net/m(?<url>[\\s\\S]*?)\"");
                            Match ma = regex.Match(k);
                            if (ma.Success)
                            {
                                string imgurl = ma.Groups["url"].Value;
                                retImages.Add(imgurl);
                            }
                            else
                            {
                                errorImage++;
                            }
                           
                        }
                    }
                    catch (Exception exp)
                    {
                        errorImage++;
                    }
                }
            }

        }

        public void GetCityName(HouseInfo info)
        {
            CityName = PinyingConvert.Convert(info.City).ToLower();
            LogCenter.LogMessage("拼音转换，城市:" + info.City + " 拼音:" + CityName ?? "");
        }

        public void MapEstate(HouseInfo info,WebRequest request)
        { 
            Random r = new Random();
            WebRequest catRequest = new WebRequest(string.Format("http://www.baixing.com/fabu/metahtml/?city={0}&category=ershoufang&city=shanghai&r={1}", CityName, r.Next(1, 99999).ToString()));
            catRequest.Cookies = loginCookies;
            string ret = catRequest.GetResouceStr();
            ret = HtmlHelper.RemoveTransferChar(HtmlHelper.DecodingJSON(ret));
            //<a href="#" key="地区_area2" id="UOm7258" value="m7258" >普陀</a>
            string localdist=HtmlHelper.RemoveAddtionalDistrict(info.LocalDistrict);
            if(localdist=="浦东")
            {
                localdist = "浦东新区";
            }

            Regex reg = new Regex("key=\"地区_area2\" id=\".*?\" value=\"(?<key>m\\d+)\"\\s*?>" + localdist + "<");

            Match ma= reg.Match(ret);
            if (ma.Success)
            {
                request.AddContent("地区_area2", localdist);
                request.AddContent("地区_area2", ma.Groups["key"].Value);
               
                string area3 = string.Format("http://www.baixing.com/ajax/tag/select/?category=zhengzu&cityEnglishName={0}&key={1}&value={2}&{3}={4}", CityName,
                    HtmlHelper.UrlEncode("地区_area2", Encoding.UTF8), HtmlHelper.UrlEncode(ma.Groups["key"].Value, Encoding.UTF8),
                     HtmlHelper.UrlEncode("地区_area2", Encoding.UTF8), HtmlHelper.UrlEncode(ma.Groups["key"].Value, Encoding.UTF8));
                catRequest = new WebRequest(area3);
                ret = catRequest.GetResouceStr();
                ret = HtmlHelper.RemoveTransferChar(HtmlHelper.DecodingJSON(ret));
                reg = new Regex("key=\"地区_area3\" id=\".*?\" value=\"(?<key>m\\d+)\"\\s*?>" + info.LocalDiduan + "<");
                //<a href="#" key="地区_area3" id="UOm2234" value="m2234" >花木</a>
                ma = reg.Match(ret);
                if (ma.Success)
                {
                    request.AddContent("地区_area3", info.LocalDiduan);
                    request.AddContent("地区_area3", ma.Groups["key"].Value);
                }
                else
                {
                    LogCenter.LogMessage("匹配地区信息失败(百姓网)。合法码:" + info.HefaCode);
                    throw new UnFindDiduanException(info.LocalDiduan);
                }
            }
            else
            {
                LogCenter.LogMessage("匹配地区信息失败(百姓网)。合法码:" + info.HefaCode);
                throw new UnFindDistrictException(info.LocalDistrict);
            }
        }
        private string GetHouseType(string number, string stypeStr)
        {
            string str = "";
            if (stypeStr == "室")
            {
                str = "4" + stypeStr + "及以上";
            }
            else
            {
                str = "3" + stypeStr + "以上";
            }
            if (number == "1")
            {
                return ("1" + stypeStr);
            }
            if (number == "2")
            {
                return ("2" + stypeStr);
            }
            if (number == "3")
            {
                return ("3" + stypeStr);
            }
            if (number == "0")
            {
                str = "0" + stypeStr;
            }
            return str;
        }
        public string GetHouseType(HouseInfo info)
        { 
            //当前 .平房=1,普通住宅=2,商住两用=3,公寓=4,别墅=5,其他=6
            //目标 普通住宅,平房, 商住两用,公寓,别墅,其他
            switch (info.HouseType)
            { 
                case "1":
                    return "平房";
                case "2":
                    return "普通住宅";
                case "3":
                    return " 商住两用";//有个空格
                case "4":
                    return "公寓";
                case "5":
                    return "别墅";
                case "6":
                    return "其他";
            }
            return "普通住宅";
        }
        private bool GetTotalPublished(string city, string houseType)
        {
            int num = new Random().Next(30, 60);
            WebRequest request = new WebRequest(string.Format("http://www.baixing.com/fabu/metahtml/?city={0}&category={1}&r={2}", city, houseType,  num));
            request.Cookies = loginCookies;
            string ret = request.GetResouceStr();
            string input = HtmlHelper.DecodingJSON(ret);
            Regex regex = new Regex(@"经纪人为(?<sumStr>\d+)条[\s\S]*?您今日已发(?<pubSumStr>\d+)条信息");
            string s = regex.Match(input).Groups["pubSumStr"].Value;
            string str4 = regex.Match(input).Groups["sumStr"].Value;
            int result = 0;
            int num3 = 0;
            if (s != "")
            {
                int.TryParse(s, out result);
            }
            if (str4 != "")
            {
                int.TryParse(str4, out num3);
            }
            if ((result >= num3) && ((result != 0) || (num3 != 0)))
            {
                return false;
            }
            return true;
        }

 

 

        public string GetDecoration(HouseInfo info)
        {
            //当前 1:毛胚 2:简装 3:精装 4:豪装
            //目标 :毛坯房,简单装修,中等装修,精装修,豪华装修
            switch (info.Decoration)
            { 
                case "1":
                    return "毛坯房";
                case "2":
                    return "简单装修";
                case "3":
                    return "精装修";
                case "4":
                    return "豪华装修";
            }
            return "简单装修";
        }

        public string GetToward(HouseInfo info)
        {
            //当前 1:东 2:南 3:西 4:北 5:南北 6:东西 7:东南 8:西南 9:东北 10:西北 11:其他
            //目标  全部是中文字符
            switch (info.Toward)
            {
                case "1":
                    return "东";
                case "2":
                    return "南";
                case "3":
                    return "西";
                case "4":
                    return "北";
            }
            return "不知道朝向";
        }

        public string GetArea(HouseInfo info)
        {
            if (info.Area.Length > 4 && info.Area.Contains("."))
            {
                return info.Area.Substring(0, 4);
            }
            return info.Area;
        }

        public string GetPrice(HouseInfo info)
        {
            if (info.Price.Length > 6 && info.Price.Contains("."))
            {
                return info.Price.Substring(0, 6);
            }
            return info.Price;
        }

        public string GetMobile(HouseInfo info)
        {
            WebRequest request = new WebRequest("http://www.baixing.com/fabu/metahtml/?city="+CityName+"&category=ershoufang&&r=9959");
            request.Cookies = loginCookies;
            string ret = request.GetResouceStr();
            //name=\"contact\" value=\"13774355335\" 
            Regex reg = new Regex("name=\\\\\"contact\\\\\" value=\\\\\"(?<mobile>\\d*?)\\\\\"");
            Match ma = reg.Match(ret);
            if (ma.Success)
            {
                return ma.Groups["mobile"].Value;
            }
            else
            {
                reg = new Regex("contactLabel\\\\\"(?<mobile>\\d*?)<");
                ma = reg.Match(ret);
                if (ma.Success)
                {
                    return ma.Groups["mobile"].Value;
                }
            }
            return info.Mobile;
        }

        public string GetHireType(HouseInfo info)
        { 
            //当前 0.单间出租 1.床位出租 2.整套出租
            //目标 整套出租 =m37617,单间合租=m37618,床位合租=m37619
            switch (info.HireType)
            { 
                case "0":
                    return "m37618";
                case "1":
                    return "m37619";
                case "2":
                    return "m37617";
            }
            return "m37617";
        }



        public string GetPayment(HouseInfo info)
        {
            ///当前 1:付一押一 2:付二押一 3:付三押一 4:付六押一
            //目标 付3押1,付1押1,付1押2,半年付,年付,月付不押,无需押金,面议
            switch (info.PayType)
            { 
                case "0":
                    return "面议";
                case "1":
                    return "付1押1";
                case "2":
                    return "付3押1";
                case "4":
                    return "半年付";
            }
            return "面议";
        }
        #endregion

        #region 编辑功能
        public override bool OpenEidt(WebBrowserOp op, HouseInfo info)
        {
            try
            {
                if (CheckLogin())
                {


                    if (info.TradeType == TradeType.Sale)
                    {
                        string post = "http://www.baixing.com/fabu/?src=header&city=shanghai&category=ershoufang";
                        foreach (KeyValuePair<string, string> k in loginCookies)
                        {  
                            WebBrowserOp.InternetSetCookie(post, k.Key, k.Value);
                        }
                        op.HouseInformation = info;
                        op.Navigate(post);
                        op.Completed += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(op_Sale_Completed);
                    }
                    else
                    {
                        string post = "http://www.baixing.com/fabu/?src=header&city=shanghai&category=zhengzu";
                        foreach (KeyValuePair<string, string> k in loginCookies)
                        {
                            WebBrowserOp.InternetSetCookie(post, k.Key, k.Value);
                        }
                        op.HouseInformation = info;
                        op.Navigate(post);
                        op.Completed += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(op_Rent_Completed);
                    }

                }
                else
                {
                    return false;
                }
            }
            catch
            {
                op.Navigate("www.baixing.com");
            }
            return true;
        }
        void op_Sale_Completed(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowserOp cl = sender as WebBrowserOp;
            HouseInfo info = cl.HouseInformation;
            cl.SetValueByName("title", info.Title);

            cl.SetValueByID("display地区_area2", info.LocalDistrict);
            cl.SetValueByID("display具体地点", info.LocalAddress);
            cl.SelectByName_Value("装修情况", GetDecoration(info));
            cl.SelectByName_Value("房型", GetHouseType(info.Room, "室"));
            cl.SelectByName_Value("房型厅", GetHouseType(info.Hall, "厅"));
            cl.SelectByName_Value("房型卫", GetHouseType(info.WashRoom, "卫"));

            cl.SetValueByName("面积", info.Area);

            cl.SetValueByName("楼层", info.CurrentFloor);

            cl.SetValueByName("价格", info.Price);
            
            cl.SetValueByType("textarea", "description", info.Description);
            cl.CheckByName_Value("faburen", "m33663", true);

            cl.SetValueByName("联系人", info.Name);
            cl.SetValueByName("contact", info.Mobile);
        }
        void op_Rent_Completed(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowserOp cl = sender as WebBrowserOp;
            HouseInfo info = cl.HouseInformation;
            cl.SetValueByName("title", info.Title);

            cl.SetValueByID("display地区_area2", info.LocalDistrict);
            cl.SetValueByID("display具体地点", info.LocalAddress);
            cl.SetValueByID("display分类", "整套出租");
            cl.SetValueByID("value分类", "m37617");

            cl.SelectByName_Value("房型", GetHouseType(info.Room, "室"));
            cl.SelectByName_Value("房型厅", GetHouseType(info.Hall, "厅"));
            cl.SelectByName_Value("房型卫", GetHouseType(info.WashRoom, "卫"));

            cl.SetValueByName("面积", info.Area);

            cl.SetValueByName("楼层", info.CurrentFloor);

            cl.SetValueByName("价格", info.MonthPrice);
            cl.SetValueByType("textarea", "description", info.Description);
            cl.CheckByName_Value("faburen", "m33663", true);

            cl.SetValueByName("联系人", info.Name);
            cl.SetValueByName("contact", info.Mobile);
        }
        #endregion
    }
}
